package com.wing.cabinet.controller.app;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.cabinet.model.entity.MessageCenter;
import com.wing.cabinet.model.response.MessageCenterVO;
import com.wing.cabinet.service.MessageCenterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.cabinet.model.request.MessageCenterForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-消息中心记录表管理", tags = "消息中心记录表管理")
@RestController("appMessageCenterController")
@RequestMapping("cabinet/app/messageCenter")
public class MessageCenterController {

    @Autowired
    private MessageCenterService messageCenterService;
    @Autowired
    private LocaleMessage localeMessage;

    @GetMapping()
    public PageResult<MessageCenterVO> list(
            @RequestParam(name = "page", value = "第几页", defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit", value = "每页多少条", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort", value="排序字段名", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "desc", required = false) String order
    ) {
        Page<MessageCenterVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        Page<MessageCenterVO> list = messageCenterService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }
    @ApiOperation(value = "查询最新的领新申请", notes = "查询")
    @GetMapping("/getNewestMessage")
    public JsonResult<MessageCenterVO> getNewestMessage(){
        MessageCenterVO messageCenterVO = messageCenterService.getNewestMessage();
        return JsonResult.success(messageCenterVO);
    }
    @ApiOperation(value = "修改消息中心记录表", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(MessageCenterForm.Update.class) @RequestBody MessageCenterForm messageCenterForm) {
        MessageCenter messageCenter = messageCenterService.getById(messageCenterForm.getId());
        if(messageCenter == null){
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(MessageCenter.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(messageCenterForm, messageCenter, copyOptions);
        if (messageCenterService.updateById(messageCenter)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long messageCenterId) {
        MessageCenterVO messageCenterVO = messageCenterService.get4appById(messageCenterId);
        if (messageCenterVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), messageCenterVO);
    }

    @PostMapping()
    public JsonResult add(@Validated MessageCenterForm messageCenterForm) {
        if (messageCenterService.add(messageCenterForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

}
